{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyN2k/iEFM9yfP4q48jNLrtB",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Huangjian2013/ai-demo/blob/main/rag/08_langchain.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "kyQKM5dZG7cq"
      },
      "outputs": [],
      "source": [
        "!pip install langchain --quiet\n",
        "!pip install langchain-openai --quiet"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from google.colab import userdata\n",
        "from langchain_openai import ChatOpenAI\n",
        "\n",
        "# 使用的是Cloudflare的OpenAPI封装接口\n",
        "model = ChatOpenAI(model=\"@cf/meta/llama-2-7b-chat-int8\",\n",
        "          api_key = userdata.get('OPEN_AI_KEY'),\n",
        "          base_url = userdata.get('OPEN_AI_BASE_URL'))"
      ],
      "metadata": {
        "id": "-dgXIBIRHH3t"
      },
      "execution_count": 12,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_core.messages import HumanMessage, SystemMessage\n",
        "\n",
        "messages = [\n",
        "    SystemMessage(content=\"将以下内容翻译成英文\"),\n",
        "    HumanMessage(content=\"ROBO-01是辆好车\"),\n",
        "]\n",
        "\n",
        "result = model.invoke(messages)\n",
        "\n",
        "print(result)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "hEs7dnwRIcKz",
        "outputId": "d6f2a97a-0c1d-4908-f6db-957b395ff6ce"
      },
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "content='ROBO-01 is a cool car.' additional_kwargs={'refusal': None} response_metadata={'token_usage': None, 'model_name': '@cf/meta/llama-2-7b-chat-int8', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None} id='run-57f49efc-6d47-4dae-80b3-fb65de2fd0ee-0'\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_core.output_parsers import StrOutputParser\n",
        "\n",
        "parser = StrOutputParser()\n",
        "\n",
        "parser.invoke(result)\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "CSdXeGs6IlCt",
        "outputId": "98955a6f-f39b-46f8-cd04-2fbb818487d1"
      },
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'ROBO-01 is a cool car.'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_core.prompts import ChatPromptTemplate\n",
        "\n",
        "prompt_template = ChatPromptTemplate.from_messages([\n",
        "    (\"system\", \"将以下内容翻译成英文\"),\n",
        "    (\"human\", \"{input}\")\n",
        "])\n",
        "chain = prompt_template | model | parser\n",
        "\n",
        "chain.invoke(\"ROBO-07也是辆好车\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "sS3ccHzvMl3c",
        "outputId": "414505f9-4371-40b2-bb7c-d6704e900d08"
      },
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'\"ROBO-07 is also a good car.\"'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 22
        }
      ]
    }
  ]
}